#!/bin/bash

# 认证API测试脚本
# 使用方法: ./scripts/test_auth_api.sh

set -e

API_BASE="http://localhost:8080"

echo "🧪 开始测试认证API..."
echo "📝 API基址: $API_BASE"
echo ""

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 1. 测试应用配置
echo -e "${YELLOW}1. 测试获取应用配置${NC}"
echo "GET /app/config"
curl -s -X GET "$API_BASE/app/config" \
  -H "Content-Type: application/json" | jq .
echo ""

# 2. 测试发送验证码
echo -e "${YELLOW}2. 测试发送验证码${NC}"
echo "POST /api/auth/send-code"
SEND_CODE_RESPONSE=$(curl -s -X POST "$API_BASE/api/auth/send-code" \
  -H "Content-Type: application/json" \
  -d '{
    "phone_number": "13800138888"
  }')

echo "$SEND_CODE_RESPONSE" | jq .
VERIFICATION_ID=$(echo "$SEND_CODE_RESPONSE" | jq -r '.verification_id // "test-id"')
echo "验证ID: $VERIFICATION_ID"
echo ""

# 3. 测试验证码登录 (使用模拟验证码)
echo -e "${YELLOW}3. 测试验证码登录${NC}"
echo "POST /api/auth/verify-code"
# 注意：在真实环境中需要使用真实的验证码
# 这里使用模拟验证码进行测试
LOGIN_RESPONSE=$(curl -s -X POST "$API_BASE/api/auth/verify-code" \
  -H "Content-Type: application/json" \
  -d '{
    "phone_number": "13800138888",
    "code": "123456",
    "verification_id": "'$VERIFICATION_ID'"
  }')

echo "$LOGIN_RESPONSE" | jq .

# 提取token用于后续请求
TOKEN=$(echo "$LOGIN_RESPONSE" | jq -r '.data.token // ""')
if [ "$TOKEN" = "" ] || [ "$TOKEN" = "null" ]; then
  echo -e "${RED}❌ 登录失败，无法获取token${NC}"
  echo "使用默认测试token..."
  TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test"
fi

echo "获取到token: ${TOKEN:0:50}..."
echo ""

# 4. 测试token验证
echo -e "${YELLOW}4. 测试token验证${NC}"
echo "GET /api/auth/validate-token"
curl -s -X GET "$API_BASE/api/auth/validate-token" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" | jq .
echo ""

# 5. 测试获取用户信息
echo -e "${YELLOW}5. 测试获取用户信息${NC}"
echo "GET /api/auth/user-info"
curl -s -X GET "$API_BASE/api/auth/user-info" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" | jq .
echo ""

# 6. 测试隧道创建 (需要认证)
echo -e "${YELLOW}6. 测试创建隧道 (需要认证)${NC}"
echo "POST /api/create/tunnel"
curl -s -X POST "$API_BASE/api/create/tunnel" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "localPort": "3000",
    "localHost": "127.0.0.1",
    "serverName": "test-tunnel"
  }' | jq .
echo ""

# 7. 测试获取隧道列表 (需要认证)
echo -e "${YELLOW}7. 测试获取隧道列表 (需要认证)${NC}"
echo "GET /api/tunnels"
curl -s -X GET "$API_BASE/api/tunnels" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" | jq .
echo ""

# 8. 测试刷新token
echo -e "${YELLOW}8. 测试刷新token${NC}"
echo "POST /api/auth/refresh-token"
curl -s -X POST "$API_BASE/api/auth/refresh-token" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" | jq .
echo ""

# 9. 测试登出
echo -e "${YELLOW}9. 测试登出${NC}"
echo "POST /api/auth/logout"
curl -s -X POST "$API_BASE/api/auth/logout" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" | jq .
echo ""

# 10. 测试无效token访问
echo -e "${YELLOW}10. 测试无效token访问${NC}"
echo "GET /api/auth/user-info (使用无效token)"
curl -s -X GET "$API_BASE/api/auth/user-info" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer invalid-token" | jq .
echo ""

echo -e "${GREEN}✅ 认证API测试完成${NC}"
echo ""
echo "📋 测试总结:"
echo "   ✓ 应用配置获取"
echo "   ✓ 验证码发送"  
echo "   ✓ 验证码登录"
echo "   ✓ Token验证"
echo "   ✓ 用户信息获取"
echo "   ✓ 隧道API认证"
echo "   ✓ Token刷新"
echo "   ✓ 用户登出"
echo "   ✓ 无效token处理"
echo ""
echo "💡 提示:"
echo "   - 确保MySQL数据库运行在 127.0.0.1:3306"
echo "   - 数据库用户名: root, 密码: root"
echo "   - 数据库名: internal_net_bridge"
echo "   - 使用 scripts/setup_database.sql 创建数据库表"


